package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.order.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * ciggar
 * create-date: 2019/7/30-上午10:04
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderShippingMapper orderShippingMapper;
    @Autowired
    OrderConverter orderConverter;

    @Override
    public GetOrderListDto getUserOrders(OrderListRequest request) {
        GetOrderListDto getOrderListDto = new GetOrderListDto();
        try {
            //检查请求参数
            request.requestCheck();
            //设置分页、排序
            if (request.getPage() != null && request.getSize() != null) {
                PageHelper.startPage(request.getPage(), request.getSize());
            }
            if (request.getSort() != null) {
                PageHelper.orderBy(request.getSort());
            }

            //获取当前用户下的所有订单
            Example orderExample = new Example(Order.class);
            orderExample.createCriteria().andEqualTo("userId", request.getUserId()).andNotEqualTo("status", 6);
            List<Order> orders = orderMapper.selectByExample(orderExample);

            //创建返回的对象
            List<OrderDetailInfo> orderDetailInfos = new ArrayList<>();
            PageInfo<Order> orderPageInfo = new PageInfo<>(orders);

            //获取每个订单下的item和shopping
            for (Order order : orders) {
                //转换订单信息
                OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);

                //获取订单下的商品信息
                List<OrderItem> orderItems = orderItemMapper.queryByOrderId(order.getOrderId());
                log.info(orderItems.toString());

                //转换商品信息
                List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);

                //获取订单下的商品物流信息
                OrderShipping orderShipping = new OrderShipping();
                orderShipping.setOrderId(order.getOrderId());
                orderShipping = orderShippingMapper.selectOne(orderShipping);

                //转换物流信息
                OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);

                //组成当前订单的参数
                orderDetailInfo.setOrderItemDto(orderItemDtos);
                orderDetailInfo.setOrderShippingDto(orderShippingDto);

                orderDetailInfos.add(orderDetailInfo);
            }
            log.info(orderDetailInfos.toString());

            //返回Json数据
            getOrderListDto.setData(orderDetailInfos);
            getOrderListDto.setTotal(orderPageInfo.getTotal());

        } catch (Exception e) {
            log.error("OrderQueryServiceImpl.getAllOrder Occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(null, e);
        }
        return getOrderListDto;
    }

    @Override
    @Transactional
    public MyOrderDetailResponse queryOrderDetail(OrderDetailRequest orderDetailRequest) {
        orderDetailRequest.requestCheck();
        MyOrderDetailResponse myOrderDetailResponse = new MyOrderDetailResponse();
        MyOrderDetailInfo myOrderDetailInfo = new MyOrderDetailInfo();

        Order order = orderMapper.selectByPrimaryKey(orderDetailRequest.getOrderId());
        //如果订单id错误即数据库没有这个id 则返回数据库异常
        if (order == null) {
            myOrderDetailResponse.setCode(OrderRetCode.DB_ORDER_EXCEPTION.getCode());
            myOrderDetailResponse.setMsg(OrderRetCode.DB_ORDER_EXCEPTION.getMessage());
            myOrderDetailResponse.setMyOrderDetailInfo(null);
            return myOrderDetailResponse;
        }

        Example example = new Example(Order.class);
        example.createCriteria().andEqualTo("userId", order.getUserId());
        //  orderTotal是总价
        BigDecimal orderTotal = order.getPayment();
        OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderDetailRequest.getOrderId());
        if (orderShipping == null) {
            myOrderDetailResponse.setCode(OrderRetCode.DB_EXCEPTION.getCode());
            myOrderDetailResponse.setMsg(OrderRetCode.DB_EXCEPTION.getMessage());
            myOrderDetailResponse.setMyOrderDetailInfo(null);
            return myOrderDetailResponse;
        }
        Example orderItemExample = new Example(OrderItem.class);
        orderItemExample.createCriteria().andEqualTo("orderId", orderDetailRequest.getOrderId());
        List<OrderItem> orderItems = orderItemMapper.selectByExample(orderItemExample);
        if (orderItems == null) {
            myOrderDetailResponse.setCode(OrderRetCode.DB_EXCEPTION.getCode());
            myOrderDetailResponse.setMsg(OrderRetCode.DB_EXCEPTION.getMessage());
            myOrderDetailResponse.setMyOrderDetailInfo(null);
            return myOrderDetailResponse;
        }

        List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);

        myOrderDetailInfo.setUserId(order.getUserId());
        myOrderDetailInfo.setOrderStatus(order.getStatus());
        myOrderDetailInfo.setUserName(orderShipping.getReceiverName());
        myOrderDetailInfo.setOrderTotal(orderTotal);
        myOrderDetailInfo.setStreetName(orderShipping.getReceiverAddress());
        myOrderDetailInfo.setTel(orderShipping.getReceiverPhone());
        myOrderDetailInfo.setGoodsList(orderItemDtos);

        myOrderDetailResponse.setMyOrderDetailInfo(myOrderDetailInfo);
        myOrderDetailResponse.setCode(OrderRetCode.SUCCESS.getCode());
        myOrderDetailResponse.setMsg(OrderRetCode.SUCCESS.getMessage());

        return myOrderDetailResponse;
    }
}
